package com.neuedu.shop.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.neuedu.shop.mapper.UserMapper;
import com.neuedu.shop.pojo.User;
import com.neuedu.shop.service.impl.UserServiceImpl;
import com.neuedu.shop.util.MybatisUtil;
import com.neuedu.shop.util.ServletUtil;



/**
 * @author MrDCG
 * @version 创建时间：2019年9月22日 下午3:33:08
 *
 */
@WebServlet("*.user")
public class UserServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7888089347449321517L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取前台的url地址
		String url = request.getRequestURL().toString();
		// 将url地址截取出需要的字段 /findAll
		String path = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
		// 设置utf-8字符集，解决中文乱码问题
		ServletUtil.getCharacterEncoding(request, response);
		// 获取session对象
		SqlSession sqlSession = MybatisUtil.getSession("mybatis-config.xml");
		PrintWriter out = response.getWriter();
		// 通过反射拿到mapper对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		UserServiceImpl userService = new UserServiceImpl(userMapper);
		if ("/findAll".equals(path)) {
			List<User> users = userService.findAll();
			request.setAttribute("users", users);
			request.getRequestDispatcher("user_list.jsp").forward(request, response);
		} else if ("/userAdd".equals(path)) {
			User user = new User();
			user.setUserName(request.getParameter("userName"));
			user.setUserPassword(request.getParameter("userPassword"));
			user.setUserPhone(request.getParameter("userPhone"));
			user.setUserAddress(request.getParameter("userAddress"));
			userService.insert(user);
			sqlSession.commit();
			// 重定向到findAll.user页面
			response.sendRedirect("findAll.user");
		} else if ("/load".equals(path)) {
			int id = Integer.parseInt(request.getParameter("id"));
			User user = userService.findById(id);
			request.setAttribute("user", user);
			request.getRequestDispatcher("user_update.jsp").forward(request, response);
		} else if ("/update".equals(path)) {
			int id = Integer.parseInt(request.getParameter("id"));
			User user = userService.findById(id);
			user.setUserName(request.getParameter("userName"));
			user.setUserPassword(request.getParameter("userPassword"));
			user.setUserPhone(request.getParameter("userPhone"));
			user.setUserAddress(request.getParameter("userAddress"));
			userService.update(user);
			sqlSession.commit();
			response.sendRedirect("findAll.user");
		} else if ("/delete".equals(path)) {
			int id = Integer.parseInt(request.getParameter("id"));
			userService.delete(id);
			sqlSession.commit();
			// 重定向
			response.sendRedirect("findAll.user");
		} else if ("/register".equals(path)) {
			String username = request.getParameter("username");
			User user = userService.findByName(username);
			if (user != null) {
				out.print("用户名已存在");
				
			} else {
				out.print("注册成功");
			}
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
}
